/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo1022;
import java.sql.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class QueryByName {
    public static void main(String args[]){
        Connection conn=null;
        PreparedStatement pstat=null;
        ResultSet rs=null;
        try{
        Class.forName("com.mysql.jdbc.Driver");//
        String url="jdbc:mysql://localhost:3306/st?characterEncoding=gbk";
        conn=DriverManager.getConnection(url, "root", "");
        pstat=conn.prepareStatement("select * from students"
                + " where name=?");
        String name=JOptionPane.showInputDialog("input name");
        pstat.setString(1,name);
        rs=pstat.executeQuery();
//           rs.next();
//        System.out.print(rs.getString(1));
//        //System.out.println(rs.getString("no"));
//        System.out.print("\t"+rs.getString(2));
//        System.out.print("\t"+rs.getInt(3));
//        System.out.print("\t"+rs.getInt(4));
//        System.out.println("\t"+rs.getInt(5));
       while(rs.next()){
        System.out.print(rs.getString("no"));
        System.out.print("\t"+rs.getString(2));
        System.out.print("\t"+rs.getInt(3));
        System.out.print("\t"+rs.getInt(4));
        System.out.println("\t"+rs.getInt(5));
       }         
        }catch(ClassNotFoundException e){
            System.out.println(e.getMessage());
        }
        catch(SQLException e){
            System.out.println(e.getMessage());
            
        }
        
        
    }
    
}
